<?php 
/**
 * eBay Label Printing Program Backoffice CMS Sub Controller 
 * This is the sub controller for Administrator manage website Documents, this controller only manage Documents 
 * at the back office , how to present the document ,leave to CMS controller 
 * @package  eBayLabelPrinter
 * @category Controller
 * @author   Norman Dong <norman.dong@hotmail.com>
 * @version  Initial
 * @copyright LeadCasket.com
 * @link     www.leadcasket.com
 * @license  Commercial
 * @todo     Only Article function partically done , need categroy function and Section functions
 */
class Backoffice_CmsController extends Zend_Controller_Action
{
	/**
	 * Index Function , doing Nothing at the moment
	 *
	 */

    public function indexAction(){
	
		echo "Back Office CMS Sub Controller";	
    
	}
	/**
	 * addArticle Function, using Article Form to add New document into Database
	 *
	 */
	
	public function addArticleAction(){
	//init article form 
	$formArticle = new Form_Cms_Article();
	$this->view->formarticle = $formArticle;
	//
	if($this->getRequest()->isPost()){
		$formData = $this->getRequest()->getPost();
		if($formArticle->isValid($formData)){	
			$newArticle = new Model_DbTable_Cms_Article();
			
			$articleTitle = $formArticle->getValue('title_article');
			$articleDate = $formArticle->getValue('title_date');
			$articlePosition = $formArticle->getValue('title_position');
			$articleFrontpage = $formArticle->getValue('title_frontpage');
			$articleSection = $formArticle->getValue('title_secion');
			$articleCategory = $formArticle->getValue('title_category');															
			$articleContent = $formArticle->getValue('title_content');
						
			$newArticle->addArticle($articleTitle, $articleDate, $articlePosition, $articleFrontpage, $articleSection, $articleCategory, $articleContent);
			}
		else{
			$formArticle->populate($formData);
			}	
		}	
		
	}
	/**
	 * editArticle Function, using Article Form to edit document 
	 *
	 */	
	
	public function editArticleAction(){
		//init article form
		$formArticle = new Form_Cms_Article();
		$this->view->formarticle = $formArticle;
		//
		if($this->getRequest()->isPost()){
			$formData = $this->getRequest()->getPost();
			if($formArticle->isValid($formData)){
				
				$updateArticle = new Model_DbTable_Cms_Article();
				$articleID = $formArticle->getValue('id_article');	
				$articleTitle = $formArticle->getValue('title_article');
				$articleDate = $formArticle->getValue('title_date');
				$articlePosition = $formArticle->getValue('title_position');
				$articleFrontpage = $formArticle->getValue('title_frontpage');
				$articleSection = $formArticle->getValue('title_secion');
				$articleCategory = $formArticle->getValue('title_category');
				$articleContent = $formArticle->getValue('title_content');
		
				$updateArticle->updateArticle($articleID, $articleTitle, $articleDate, $articlePosition, $articleFrontpage, $articleSection, $articleCategory, $articleContent);
			}
			else{
				$formArticle->populate($formData);
			}
		}
		else{
			
			$articleID = $this->_getParam('articleid');
			$articleList = new Model_DbTable_Cms_Article();
			$formArticle->populate($articleList->getArticle($articleID));
		}
		
	}	
	
	/**
	 * deleteArticle Function, delete website document from the Database
	 *
	 */

	public function deleteArticleAction(){
		
		$articleID = $this->_getParam('articleid');
		$articleList = new Model_DbTable_Cms_Article();
		$articleList->deleteArticle($articleID);
	
		$this->view->articleid = $articleID;
		
	}	
	
	/**
	 * list Article Function, using Jquery table list to list Article to manage
	 *
	 */
	
	public function listArticleAction(){
		$articleList = new Model_DbTable_Cms_Article();
		$this->view->articleList = $articleList->listArticle();
	}
	
	/**
	 * addSection Function, using Section Form to add New Section into Database
	 * Section is the root category of the document structure
	 *
	 */

	public function addSectionAction(){
		$formSection = new Form_Cms_Articlesection();
		$this->view->formsection = $formSection;
		
		if($this->getRequest()->isPost()){
			$formData = $this->getRequest()->getPost();
			if($formSection->isValid($formData)){
				$addSection = new Model_DbTable_Cms_Articlesection();
				
				$sectionTitle = $formSection->getValue('title_section_article');
				$sectionPosition = $formSection->getValue('position_section_article');
				$available = $formSection->getValue('available_section_article');
				
				$addSection->AddArticleSection($sectionTitle, $sectionPosition, $available);
				
			}
			else{
				$formSection->populate($formData);
			}		
		}
	}
	/**
	 * editSection Function, using Section Form to edit Section in the  Database
	 * Only section Name/Order can be modified
	 *
	 */
	public function editSectionAction(){
		
		$formSection = new Form_Cms_Articlesection();
		$this->view->formsection = $formSection;
		if($this->getRequest()->isPost()){
			$formData = $this->getRequest()->getPost();
			if($formSection->isValid($formData)){
				$updateSection = new Model_DbTable_Cms_Articlesection();
				
				$sectionID = $formSection->getValue('id_section_artice');
				
				$sectionTitle = $formSection->getValue('title_section_article');
				$sectionPosition = $formSection->getValue('position_section_article');
				$available = $formSection->getValue('available_section_article');
				
				$updateSection->updateArticleSection($sectionID, $sectionTitle, $sectionPosition, $available);
				
			}
			else{
				
				$formSection->populate($formData);
			}
		}
		else{
			$sectionID = $this->_getParam('sectionid');
			$articleSection = new Model_DbTable_Cms_Articlesection();
			$formSection->populate($articleSection->getArticleSection($sectionID));
		}		
		
	
	}	
	/**
	 * deleteSection Function, delete Section from the Database
	 * if section is Not empty , then you can not delete section 
	 * Not empty means there is category & Article allocate to the section 
	 * Section is the root category of the document structure
	 *
	 */		
	public function deleteSectionAction(){
	
		$errorFlag = 0;
		//check category empty 
		$sectionID = $this->_getParam('sectionid');
		$articleCategory = new Model_DbTable_Cms_Articlecategory();
		if($articleCategory->listCategoryBySection($sectionID)){
			$errorFlag = 1;
		}
		unset($articleCategory);
		$articleCms = new Model_DbTable_Cms_Article();
		if($articleCms->listArticleBySection($sectionID)){
			
			$errorFlag = 2;
		}
		unset($articleCms);
		if(!$errorFlag){
			
			$articleSection = new Model_DbTable_Cms_Articlesection();
			$articleSection->deleteArticleSection($sectionID);
			unset($articleSection);
		}
		else {
			//show error message 
			$this->view->errorflag = $errorFlag;
			
		}
			
		
		//check article empty 
	
		
	}

	/**
	 * listSection Function, List Sections to be modify
	 *
	 */
	public function listSectionAction(){
		$articleSection = new Model_DbTable_Cms_Articlesection();
		$this->view->articlesectionlist = $articleSection->listSection();	
	}	
	/**
	 * addCategory Function, using Category Form to add new category into Database
	 * Category is the second level in the document structure 
	 *
	 */
	public function addCategoryAction(){
		
		$formCategory = new Form_Cms_Articlecategory();
		$this->view->formCategory = $formCategory;
		
		if($this->getRequest()->isPost()){
			$formData = $this->getRequest()->getPost();
			if($formCategory->isValid($formData)){

				$addCategory = new Model_DbTable_Cms_Articlecategory();
				$articleCategoryTitle = $formCategory->getValue('title_category_article');
				$articleCategoryPosition = $formCategory->getValue('position_category_article');
				$sectionID = $formCategory->getValue('id_section_category_article');
				$available = $formCategory->getValeu('available_category_article');
				
				$addCategory->addArticleCategory($articleCategoryTitle, $articleCategoryPosition, $sectionID, $available);
				
			}
			else{
				$formCategory->populate($formData);
			}
		}
		
	}
	/**
	 * edit Category Function, edit the document category
	 * Category is the second level in the document structure
	 *
	 */
	
	public function editCategoryAction(){
		
		$formCategory = new Model_DbTable_Cms_Articlecategory();
		$this->view->formCategory = $formCategory;
		
		if($this->getRequest()->isPost()){
			$formData = $this->getRequest()->getPost();
			if($formCategory->isValid($formData)){
		
				
				$updateCategory = new Model_DbTable_Cms_Articlecategory();
				
				$articleCategoryID = $formCategory ->getValue('id_category_article');
				$articleCategoryTitle = $formCategory->getValue('title_category_article');
				$articleCategoryPosition = $formCategory->getValue('position_category_article');
				$sectionID = $formCategory->getValue('id_section_category_article');
				$available = $formCategory->getValeu('available_category_article');
		
				$updateCategory->updateArticleCategory($articleCategoryID, $articleCategoryTitle, $articleCategoryPosition, $sectionID, $available);
		
			}
			else{
				$formCategory->populate($formData);
			}
			
		}
		else{
				$articleCategoryID = $this->_getParam('categoryid');
				$articleCategory = new Model_DbTable_Cms_Articlecategory();
				$formCategory->populate($articleCategory->getArticleCategory($articleCategoryID));
			
		}
			
	}	
	/**
	 * delete Category Function, delete the document category
	 * same as the section , if the current category is Not empty , then it should not be deleted
	 * Category is the second level in the document structure 
	 *
	 */
	public function deleteCategoryAction(){
	
		//check if anyarticle signed to this category 
		$errorFlag = 0;
		
		$categoryID = $this->_getParam('categoryid');
		$articleList = new Model_DbTable_Cms_Article();
		
		if($articleList->listArticleByCategory($categoryID)){
			
			$errorFlag = 1;
		
		}
		
		if(!$errorFlag){	
			$categoryArticle = new Model_DbTable_Cms_Articlecategory();
			$categoryArticle->deleteArticleCategory($categoryID);	
			unset($categoryArticle);
			
		}
		$this->view->errorFlag = $errorFlag;
		
		unset($articleList);	
			
	}

	
	/**
	 * list  Category Function, list document category according the section
	 * secion & category should be list together
	 * Category is the second level in the document structure
	 *
	 */	
	public function listCategoryAction(){
		
		$categoryArticle = new Model_DbTable_Cms_Articlecategory();
		$this->view->articlecategorylist = $categoryArticle->listCategory();
		
	}			

}
?>